﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.admin.Edit_Entities
{
    public partial class EditUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ErrorLabel.Visible = false;
        }


        protected void UserDetailsView_DataBound(object sender, EventArgs e)
        {
            Label type = (Label)UserDetailsView.FindControl("TypeLabel");

            if (type != null)
            {
                UserTypeBLL typeBLL = new UserTypeBLL();
                try
                {
                    type.Text = typeBLL.UserTypeGetByNum(new Guid(type.Text))[0].user_type;
                }
                finally
                {
                    //TODO
                    //we should add dispose here
                    // we need to add dispose function foreach bll
                    // or maybe the detailsview disposes alone ,
                    // need to check, because there exists disposing event
                }
            }
        }

        protected void UsersGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            UserDetailsView.PageIndex = UsersGridView.SelectedIndex;
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            UserBLL BLL = new UserBLL();
            String Previous = BLL.userGetByNum((Guid)UserDetailsView.SelectedValue)[0].user_id;

                if (Previous != args.Value)
                {
                    try
                    {
                        if (BLL.userGetByID(args.Value).Rows.Count > 0)
                            args.IsValid = false;
                        else
                            args.IsValid = true;
                    }
                    finally
                    {
                        //TODO: Dispose
                    }
                
            }
        }

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            UserBLL BLL = new UserBLL();
            String Previous = BLL.userGetByNum((Guid)UserDetailsView.SelectedValue)[0].user_name;


            if (Previous != args.Value)
            {
                try
                {
                    if (BLL.UserGetByName(args.Value).Rows.Count > 0)
                        args.IsValid = false;
                    else
                        args.IsValid = true;
                }
                finally
                {
                    //TODO: Dispose
                }
            }
        }

        protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
        {
            UserBLL BLL = new UserBLL();
            String Previous = BLL.userGetByNum((Guid)UserDetailsView.SelectedValue)[0].user_login_name;


            if (Previous != args.Value)
            {
                try
                {
                    if (BLL.userGetByLogin(args.Value).Rows.Count > 0)
                        args.IsValid = false;
                    else
                        args.IsValid = true;
                }
                finally
                {
                    //TODO: Dispose
                }

            }
        }

        protected void UserDetailsView_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            try
            {
                Label Label = (Label)UserDetailsView.FindControl("UserLabel");
                UserBLL BLL = new UserBLL();
                Guid guid = BLL.UserGetByName(Label.Text)[0].user_num;

                LendBLL CBLL = new LendBLL();
                if (CBLL.LendGetByUser(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible = true;
                }

                DeliveredBLL DBLL = new DeliveredBLL();
                if (DBLL.DeliveredGetByUser(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible = true;
                }


            }
            finally
            {
                //TODO: Dispose
            }
        }


       

    }
}
